// pages/community/community.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbarHeight:"",
    width:"108px",
    
    
    // 状态栏高度
    statusBarHeight: wx.getStorageSync('statusBarHeight') + 'px',
    // 导航栏高度
    navigationBarHeight: wx.getStorageSync('navigationBarHeight') + 'px',
    // 胶囊按钮高度
    menuButtonHeight: wx.getStorageSync('menuButtonHeight') + 'px',
    // 导航栏和状态栏高度
    navigationBarAndStatusBarHeight:
      wx.getStorageSync('statusBarHeight') +
      wx.getStorageSync('navigationBarHeight') +
      'px',
    topPostList:[],
    plainPostList:[],
    list: [
      {
        "text": "首页",
        
        "iconPath": "/images/home.png",
        "selectedIconPath": "/images/home_active.png"
      },
      {
        "text": "文库",
        
        "iconPath": "/images/library.png",
        "selectedIconPath": "/images/library_active.png"
      },
      {
        "text": "社区",
        
        "iconPath": "/images/community.png",
        "selectedIconPath": "/images/community_active.png"
      },
      {
        "text": "我的",
        
        "iconPath": "/images/user.png",
        "selectedIconPath": "/images/user_active.png"
      }
    ]
  },

  
  async onLoad(options) {
    let index = options.pageIndex
    this.setData({pageIndex : index})
    this.getPosts()
  },
  
  getPosts:function(){
    let app = getApp()
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.url + '/community/post',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: result => {
        let postList = result.data.data
        let topPostList = new Array()
        let plainPostList = new Array()
        for(let i = 0; i < postList.length; ++i){
          if(postList[i].type == 0){
            postList[i].coverImg = app.globalData.url + "/" + postList[i].coverImg
          }else if(postList[i].type == 1){
            if(postList[i].coverImg != ""){
              postList[i].coverImg = postList[i].coverImg.split(",")
              for(let j = 0; j < postList[i].coverImg.length; ++j){
                postList[i].coverImg[j] = app.globalData.url + "/" + postList[i].coverImg[j]
              }
            }else{
              postList[i].coverImg = []
            }
          }
          if(postList[i].imgPath != ""){
            postList[i].imgPath = postList[i].imgPath.split(",")
            for(let j = 0; j < postList[i].imgPath.length; ++j){
              postList[i].imgPath[j] = app.globalData.url + "/" + postList[i].imgPath[j]
            }
          }else{
            postList[i].imgPath = []
          }
          if(postList[i].type == 0){
            topPostList.push(postList[i])
          }else if(postList[i].type == 1){
            plainPostList.push(postList[i])
          }
        }
        this.setData({
          topPostList:topPostList,
          plainPostList:plainPostList,
        })
        wx.hideLoading()
      }
    })
  },
  
  onReady: function () {
    let width = wx.getSystemInfoSync().windowWidth;
    let imgWidth = Math.floor((width - 50) / 3)
    this.setData({width:imgWidth+"px"})

    //计算tabbar高度
    let tabbarHeight = wx.getStorageSync('tabBarHeight')
    if(tabbarHeight == 0){
      console.log(tabbarHeight)
      tabbarHeight = 82
    }
    this.setData({tabbarHeight: tabbarHeight + "px"})
  },
  goToSearch:function () {
    wx.navigateTo({
      url: '/pages/search/search?source=community',
    })
  },
  goToUserDetail:function (e) {
    let userId = e.currentTarget.dataset.openId
    wx.navigateTo({
      url: '/pages/user/user?openId=' + userId,
    })
  },
  previewImgs:function(e){
    let src = e.currentTarget.dataset.src
    let arr = new Array()
    arr.push(src)
    wx.previewImage({
      current: src,
      urls: arr
    })
  },
  goToDetail:function(event){
    let app = getApp()
    let postIndex = event.currentTarget.dataset.postIndex
    let type = event.currentTarget.dataset.type
    if(type == 0){
      app.globalData.selectedPost = this.data.topPostList[postIndex]
    }else if(type == 1){
      app.globalData.selectedPost = this.data.plainPostList[postIndex]
    }
    wx.navigateTo({
      url: '/pages/detail/detail?type='+type+"&index="+postIndex,
    })


  },
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2
      })
    }
  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh() {  
    this.getPosts()
    wx.stopPullDownRefresh()
    
  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})